from pms.common.views import PostFormula
from info.models import Mail
from info.utils.mail import mail_list_view


class API(PostFormula):
    def handle(self, req):
        mails = Mail.objects.filter(mailreceive__uid=self.user, mailreceive__is_delete=False)
        if 'did' in req and req['did'] is not None:
            mails = mails.filter(did_id=req['did'])
        elif 'pid' in req and req['pid'] is not None:
            mails = mails.filter(pid_id=req['pid'])
        elif 'system' in req and req['system'] is True:
            mails = mails.filter(did=None, pid=None)
        return self.success_respond({
            'list': mail_list_view(mails)
        })
